<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@page import="org.jeecgframework.web.util.StaticValue" %>
<%@include file="/context/mytags.jsp" %>
<t:base type="jquery,easyui,tools,DatePicker"></t:base>
<div class="easyui-layout" fit="true">
    <div region="center" style="padding:1px;">
        <t:datagrid name="cbsServiceFacilitaorList" checkbox="true"
                    title="服务商商务任务" actionUrl="cbsServiceAssignListController.do?facilitator" pageList="[20,100,200]"
                    fitColumns="false" idField="id" queryMode="group" autoLoadData="false" onLoadSuccess="changetxt">
            <t:dgCol title="主键" field="id" hidden="true" queryMode="single" width="120"></t:dgCol>
            <t:dgCol title="设备SN" field="sn" queryMode="single" width="120" query="true"></t:dgCol>
            <t:dgCol title="服务单号" field="code" queryMode="single" width="120" query="true"
                     url="cbsServiceAssignListController.do?detailsOfService&id={id}"
                     funname="CbsserviceDetials"></t:dgCol>
            <t:dgCol title="工程师" field="engineerName" queryMode="single" width="80" query="true" ></t:dgCol>
            <t:dgCol title="服务商处理时长" field="dealWithTiem" queryMode="single" width="105"></t:dgCol>
            <t:dgCol title="服务类型" field="serviceType" queryMode="single" width="85" query="true"></t:dgCol>
            <t:dgCol title="级别" field="level" queryMode="single" width="40"
                     dictionary="<%=StaticValue.serviceLevel%>"></t:dgCol>
            <t:dgCol title="处理进度" field="speedStatus" queryMode="single" width="120"
                     dictionary="<%=StaticValue.Third_Party_Processing_Progress %>"></t:dgCol>
            <t:dgCol title="服务商处理进度" field="speedstatus1" hidden="true" query="true"></t:dgCol>
            <t:dgCol title="服务商处理进度" field="speedstatus2" hidden="true" query="true" extend="{class:easyui-combobox}"></t:dgCol>
            <t:dgCol title="服务单类型" field="sourceType" queryMode="single" width="120"
                     dictionary="<%=StaticValue.Source_Type%>" hidden="true"></t:dgCol>
            <t:dgCol title="影院名称" field="intheaterName" queryMode="single" width="180" query="true"></t:dgCol>
            <t:dgCol title="院线" field="typename" queryMode="single" width="60" query="true"></t:dgCol>
            <t:dgCol title="省" field="provinces" queryMode="single" width="120" query="true"></t:dgCol>
            <t:dgCol title="市" field="city" queryMode="single" width="120"></t:dgCol>
            <t:dgCol title="联系人姓名" field="contactsName" queryMode="single" width="80"></t:dgCol>
            <t:dgCol title="联系人手机" field="phone" queryMode="single" width="100" query="true"></t:dgCol>
            <t:dgCol title="派单时间" field="assignTime" formatter="yyyy-MM-dd hh:mm" queryMode="group" width="120" query="true"></t:dgCol>
            <t:dgCol title="预约上门时间" field="orderTime" formatter="yyyy-MM-dd" queryMode="group" width="110"></t:dgCol>
           <t:dgCol title="现场完工时间" field="finishTime" formatter="yyyy-MM-dd hh:mm" queryMode="group" width="120"></t:dgCol>
            <t:dgCol title="系统完工时间" field="closeTime" formatter="yyyy-MM-dd hh:mm" queryMode="group" width="120"
                     query="true"></t:dgCol>
            <t:dgCol title="备件申请" field="apply" queryMode="single" width="70" query="true"
                     dictionary="<%=StaticValue.DIC_TRUE_OR_FALSE_NUM %>"></t:dgCol>
            <t:dgCol title="备件预计到货时间" field="expecttime" queryMode="single" width="125"></t:dgCol>
            <t:dgCol title="SO号" field="SO" queryMode="single" width="80" query="true"></t:dgCol>
            <t:dgToolBar title="派单" icon="icon-add" funname="findEngineerPeople"></t:dgToolBar>
            <t:dgToolBar title="拒绝" icon="icon-add" funname="refuseEngineerPeoples"></t:dgToolBar>
            <t:dgToolBar title="补填完工单" icon="icon-add" funname="tpAssignEngineerEditServiceReport"></t:dgToolBar>
            <t:dgToolBar title="导出" icon="icon-putout" funname="ExportXls"></t:dgToolBar>
        </t:datagrid>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        //给时间控件加上样式
        $("#cbsServiceFacilitaorListtb").find("input[name='closeTime_begin']").attr("class", "Wdate").attr("style", "height:20px;width:90px;").click(function () {
            WdatePicker({dateFmt: 'yyyy-MM-dd'});
        });
        $("#cbsServiceFacilitaorListtb").find("input[name='closeTime_end']").attr("class", "Wdate").attr("style", "height:20px;width:90px;").click(function () {
            WdatePicker({dateFmt: 'yyyy-MM-dd'});
        });
        $("#cbsServiceFacilitaorListtb").find("input[name='assignTime_begin']").attr("class", "Wdate").attr("style", "height:20px;width:90px;").click(function () {
            WdatePicker({dateFmt: 'yyyy-MM-dd'});
        });
        $("#cbsServiceFacilitaorListtb").find("input[name='assignTime_end']").attr("class", "Wdate").attr("style", "height:20px;width:90px;").click(function () {
            WdatePicker({dateFmt: 'yyyy-MM-dd'});
        });
    });

    $(function () {
        territoryLinks('cbsServiceFacilitaorList', 'serviceType', 1);
        territoryLink('cbsServiceFacilitaorList', 'provinces', 1);

        //服务商处理进度
        $("div[name='searchColums'] span:nth-child(5)").hide();
        $("#speedstatus2").combobox({
            data: [{
                "id": 0,
                "text": "HD待分派",
                "selected": true
            }, {
                "id": 1,
                "text": "第三方已拒绝",
                "selected": true
            }, {
                "id": 2,
                "text": "已分派第三方",
                "selected": true
            }, {
                "id": 3,
                "text": "第三方已分配",
                "selected": true
            }, {
                "id": 4,
                "text": "区域工程师已分配",
                "selected": true
            }, {
                "id": 5,
                "text": "工程师已接受",
                "selected": true
            }, {
                "id": 6,
                "text": "工程师实施中",
                "selected": true
            }, {
                "id": 7,
                "text": "服务已完成"
            }],
            valueField: 'id',
            textField: 'text',
            editable: false,
            multiple: true,
            onLoadSuccess: function(){
                $("[name='speedstatus1']").val($(this).combobox('getValues'));
                setTimeout(function(){
                    cbsServiceFacilitaorListsearch();
                },0);
            },
            onSelect: function (row) { //选中一个选项时调用
                //获取选中的值的values
                $("[name='speedstatus1']").val($(this).combobox('getValues'));
            },
            onUnselect: function (row) {//不选中一个选项时调用
                //获取选中的值的values
                $("[name='speedstatus1']").val($(this).combobox('getValues'));
            }
        });
    });

    function searchReset() {
        addOneTab("服务商任务", "cbsServiceAssignListController.do?facilitatorlist", "");
    }

    //导出
    function ExportXls() {
    	lockmask(true);
        var currPageRows = $("#cbsServiceFacilitaorList").datagrid("getRows");
        var idsData = [];
        $(currPageRows).each(function (i, rowData) {
            idsData.push(rowData.id);
        });
        if (idsData.length > 0) {
            myDownload("cbsServiceController.do?exportInspectionAssignList", "planIds=" + idsData.join(","));
            var timer = setInterval(function(){
                $.ajax({
                    url: "cbsServiceController.do?getShadeKey",
                    cache: false,
                    async:false,
                    dataType: 'json',
                    type: 'POST',
                    error: function () {// 请求失败处理函数
                    },
                    success: function (data) {
                        if (data == 0) {
                            lockmask(false);
                            clearInterval(timer);
                        }
                    }
                });
            }, 1000);
        } else {
            tip("当前页面没有数据可以导出");
        }

    }

    //服务商商务派单
    function findEngineerPeople() {
        var datas = $("#cbsServiceFacilitaorList").datagrid("getSelections");
        if (datas.length == 0) {
            tip("请选择要分派的服务单!");
            return;
        }
        var planIdArr = [];
        $.each(datas, function (i, item) {
            planIdArr.push(item.id);
        });
        var planIds = planIdArr.join(",");
        if (judge(planIds)) {
            $.ajax({
                url: "cbsServiceAssignListController.do?inspect",
                data: {
                    "planIds": planIds
                },
                cache: false,
                dataType: 'json',
                type: 'POST',
                async: true,
                beforeSend: function () {
                    lockmask(true);
                },
                success: function (data) {
                    if (data.success) {
                        createwindow("分派", "cbsServiceAssignListController.do?findEngineerPeopleList&planIds=" + planIds, 600, 500);
                    } else {
                        tip(data.msg);
                    }
                },
                complete: function () {
                    lockmask(false);
                }
            });
        } else {
            tip("操作人与派单人不符，请刷新系统！");
        }
    }

    //拒绝
    function refuseEngineerPeoples() {
        var datas = $("#cbsServiceFacilitaorList").datagrid("getSelections");
        if (datas.length == 0) {
            tip("请选择要拒绝的服务单!");
            return;
        }
        var planIdArr = [];
        $.each(datas, function (i, item) {
            planIdArr.push(item.id);
        });
        var planIds = planIdArr.join(",");
        if (judge(planIds)) {
            $.ajax({
                url: "cbsServiceAssignListController.do?inspectRefuse",
                data: {
                    "planIds": planIds
                },
                cache: false,
                dataType: 'json',
                type: 'POST',
                async: true,
                beforeSend: function () {
                    lockmask(true);
                },
                success: function (data) {
                    if (data.success) {
                        createwindow("拒绝", "cbsServiceAssignListController.do?refuseEngineerPeopleList&planIds=" + planIds, 300, 200);
                    } else {
                        tip(data.msg);
                    }
                },
                complete: function () {
                    lockmask(false);
                }
            });
        } else {
            tip("操作人与派单人不符，请刷新系统！");
        }
    }

    /**
     * @Author Sunliangliang 2018/10/10 18:17
     * @Description 跳转 补填完工单页面
     * @params
     */
    function tpAssignEngineerEditServiceReport(){
        var datas = $("#cbsServiceFacilitaorList").datagrid("getSelections");
        if (datas.length != 1) {
            tip("请选择一个要补填的服务单!");
            return;
        }
        var csid = datas[0].id;
        $.ajax({
            /*获取不同的服务单类型*/
            url: "cbsServiceAssignListController.do?canEditServiceReport",
            data: {
                "id": csid
            },
            cache: false,
            dataType: 'json',
            async: false,
            type: 'POST',
            success: function (data) {
                if (data.success){
                    $.ajax({
                        /*获取不同的服务单类型*/
                        url: "cbsServiceAssignListController.do?doorToDoorCheckIn",
                        data: {
                            "id": csid
                        },
                        cache: false,
                        dataType: 'json',
                        async: false,
                        type: 'POST',
                        success: function (data) {
                            if (data.success) {
                                var type = data.attributes['type'];
                                if (type == 'type-yqfx') {//服务支持 > 现场检测
                                    addOneTab('引擎翻新现场检测完工单', 'cbsServiceReportController.do?goEngineRefurbished&id=' + csid + '&type=2&editReport=true');
                                }
                                if (type == 'type-yqfxys') {//增值服务 > 引擎翻新
                                    addOneTab('引擎翻新验收单', 'cbsServiceReportController.do?goEngineRefurbishedSign&id=' + csid + '&type=2&editReport=true');
                                }
                                if (type == 'type-ggfx') {//增值服务 > 光管翻新
                                    addOneTab('光管翻新验收单', 'cbsServiceReportController.do?goLighttubeAcceptance&id=' + csid + '&type=2&editReport=true');
                                }
                                if (type == 'type-jszc') {//服务支持 > 技术支持
                                    addOneTab('技术支持完工单', 'cbsServiceReportController.do?goTechnologyAgree&id=' + csid + '&type=2&editReport=true');
                                }
                                if (type == 'type-yj') {//服务支持 > 现场检测
                                    $.ajax({
                                        url : "cbsDelinerController.do?judgeService",
                                        data : {"id" :csid},
                                        async:false,
                                        cache : false,
                                        dataType : 'json',
                                        type : 'POST',
                                        success : function(data){
                                            if("type-yj" == data.msg){//服务支持 > 现场检测
                                                addOneTab('现场检测完工单', 'cbsServiceReportController.do?goReferDetection&id=' + csid+'&type=2&editReport=true');
                                            }else if("type-yqfx" == data.msg){//服务支持 > 现场检测
                                                addOneTab('引擎翻新现场检测完工单', 'cbsServiceReportController.do?goEngineRefurbished&id=' + csid+'&type=2&editReport=true');
                                            }
                                        }
                                    });
                                }
                                if (type == 'type-wx') {//故障维修
                                    $.ajax({
                                        url: "cbsServiceReportController.do?goCbsServiceRepairReport",
                                        data: {
                                            "id": csid
                                        },
                                        cache: false,
                                        dataType: 'json',
                                        async: false,
                                        type: 'POST',
                                        success: function (data) {
                                            var attributes = data.attributes;
                                            if (attributes.reportCategory == 1) {//故障维修
                                                addOneTab('现场服务完工报告', 'cbsServiceReportController.do?cbsServiceReportXC&id=' + csid + '&type=2&editReport=true');
                                            }
                                            if (attributes.reportCategory == 2) {//故障维修
                                                addOneTab('远程服务完工报告', 'cbsServiceReportController.do?RemoteService&id=' + csid + '&type=2&editReport=true');
                                            }
                                            if (attributes.reportCategory == -1) {
                                                openwindow('是否现场服务', 'cbsServiceReportController.do?fieldservice&id=' + csid + '&type=2', '', 350, 200);
                                            }
                                        }
                                    })

                                }
                                if (type == 'type-xcjc') {//服务支持 > 维保检测
                                    addOneTab('现场检测完工单', 'cbsServiceReportController.do?goReferDetection&id=' + csid + '&type=2&editReport=true');
                                }
                            } else {
                                top.window.tip(data.msg);
                            }

                        }
                    });
                }else{
                    tip(data.msg)
                }
            }
        });
    }

    function CbsserviceDetials(filedName, url) {
        addOneTab("服务订单详情", url);

    }

    /*判断派单人是否是当前登录人*/
    function judge(planIds) {
        var flag = false;
        $.ajax({
            url: "cbsServiceController.do?judgePeople",
            data: {
                "cbsId": planIds,
                "type": "1"
            },
            async: false,
            cache: false,
            dataType: 'json',
            type: 'POST',
            error: function () {// 请求失败处理函数
            },
            success: function (data) {
                if (data.success) {
                    flag = true;
                }
            }
        });

        return flag;
    }
    
    /*处理文字*/
    function changetxt() {
        var trs = $("#cbsServiceFacilitaorList").prev().find('div.datagrid-body').find('tr');
        for (var i = 0; i < trs.length; i++) {
        	if (trs[i].cells[4].firstChild.innerHTML == 'null') {
        		trs[i].cells[4].firstChild.innerHTML = '';
        	} else{
              trs[i].cells[4].firstChild.innerHTML = trs[i].cells[4].firstChild.innerHTML + '小时';
        	}
        }
     }
</script>